﻿namespace PillBox.Client.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using PillBox.Client.Framework;

    public class ScheduleDayViewModel : ViewModelBase
    {
        public ScheduleDayViewModel()
            : this(DateTime.Today, null)
        {
        }

        public ScheduleDayViewModel(DateTime date, IEnumerable<ScheduleDayItemViewModel> daySchedule)
        {
            this.Date = date;

            IEnumerable<ScheduleDayGroup> groups = null;

            if (daySchedule != null)
            {
                var schedule = daySchedule.ToList();

                //if (this.Date < DateTime.Today)
                //{
                //    schedule.ForEach(item => item.IsTaken = true);
                //    schedule[3].IsTaken = false;
                //}
                //else if (this.Date == DateTime.Today)
                //{
                //    schedule[0].IsTaken = true;
                //    schedule[0].TakenTimestamp = new DateTime(2010, 4, 17, 7, 10, 0);
                //    schedule[1].IsTaken = true;
                //    schedule[1].TakenTimestamp = new DateTime(2010, 4, 17, 7, 10, 0);
                //}

                this.IsMultiPerson = schedule.GroupBy(x => x.Person).Count() == 1;

                groups = schedule
                    .GroupBy(x => x.TimeOfDay)
                    .Select(g => new ScheduleDayGroup { Key = g.Key, Values = g });
            }

            this.GroupedDaySchedule = groups;

            RaisePropertyChanged(() => Date);
            RaisePropertyChanged(() => AllowEditing);
        }

        public bool IsMultiPerson
        {
            get;
            private set;
        }

        public bool IsToday
        {
            get
            {
                return this.Date == DateTime.Today;
            }
        }

        public DateTime Date
        {
            get;
            set;
        }

        public IEnumerable<ScheduleDayGroup> GroupedDaySchedule
        {
            get;
            set;
        }

        public bool AllowEditing
        {
            get
            {
                return this.Date == DateTime.Today || this.Date == DateTime.Today.AddDays(-1);
            }
        }
    }

    /// <summary>
    /// Wrapper for CustomGroup&lt;DateTime, ScheduleDayItemViewModel&gt; to enable design time data.
    /// </summary>
    public class ScheduleDayGroup : CustomGroup<DateTime, ScheduleDayItemViewModel>
    {
    }
}
